/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.jsfconsole.util.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import com.gregbugaj.jsfconsole.util.ReflectUtil;

/**
 * Reflection provider that serialized all the fields of the objects
 * @author devil
 *
 */
public class DefaultReflectionProvider implements ReflectionProviderStrategy {
	
	public Field getAccesibleField(String fieldName, Object obj) {
		for(Class<?> superClazz=obj.getClass(); superClazz!=Object.class; superClazz=superClazz.getSuperclass()){		
			for(Field field : superClazz.getDeclaredFields()){
				if(!ReflectUtil.makeAccessible(field)){
					continue;
				}
				if(field.getName().equals(fieldName)){					
					return field;
				}
			}
		}
		return null;
	}

	
	public List<Field> getAccesibleFields(Object obj) {
		List<Field> items=new ArrayList<Field>();
		for(Class<?> superClazz=obj.getClass(); superClazz!=Object.class; superClazz=superClazz.getSuperclass()){		
			for(Field field : superClazz.getDeclaredFields()){
				
				if(!(modifiersSupported(field)  && ReflectUtil.makeAccessible(field))){
					continue;
				}
				
				items.add(field);			
			}
		}
		return items;
	}

	/**
	 * Check if we have access to the field
	 * @param field
	 * @return
	 */
	protected boolean modifiersSupported(Field field) {
        return !(field.isSynthetic() ||
        		Modifier.isStatic(field.getModifiers())
                || Modifier.isTransient(field.getModifiers())
                );
    }

	
	
	public List<Method> getMethods(Class<?> clazz) {
		List<Method> items=new ArrayList<Method>();
		for(Class<?> superClazz=clazz;superClazz!=Object.class; superClazz=superClazz.getSuperclass()){
			for(Method method : superClazz.getDeclaredMethods()){	
				if(method.isSynthetic() || 
						Modifier.isPrivate(method.getModifiers()) ||	
						method.getParameterTypes().length!=0 ||
						method.getReturnType() == Void.TYPE
				){
					continue;
				}

				items.add(method);
			}
		}
		return items;
	}
}
